//
//  JPTabBarController.m
//  JPKY
//
//  Created by Vitas on 2016/11/13.
//  Copyright © 2016年 Vitas. All rights reserved.
//

#import "JPTabBarController.h"
#import "JPHomeViewController.h"
#import "JPHealthViewController.h"
#import "JPFindViewController.h"
#import "JPNavigationController.h"
#import "HMAdditions.h"

@interface JPTabBarController ()

@end

@implementation JPTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


/**
 重写init方法,初始化根控制器
 */
-(instancetype)init{
    if (self = [super init]) {
        
        [self setController];
    }
    
    return self;
}


/**
 设置TabBar的控制器
 */
-(void)setController {
    
    NSString *home = @"首页";
    NSString *find = @"发现";
    NSString *health = @"健康";
    
    // 首页
    JPHomeViewController *homeVC = [[JPHomeViewController alloc]init];
    [self addChildViewController:homeVC titles:home images:@"tab_normal_1"];
    [self setBarItem:homeVC];
    
    // 发现
    JPFindViewController *findVC  = [[JPFindViewController alloc]init];
    [self addChildViewController:findVC titles:find images:@"tab_normal_2"];
    [self setBarItem:findVC];
    
    // 健康
    JPHealthViewController *healthVc = [[JPHealthViewController alloc]init];
    [self addChildViewController:healthVc titles:health images:@"tab_normal_3"];
    [self setBarItem:healthVc];
}


/**
 设置NavigationBarItem的显示样式
 */
-(void)setBarItem:(UIViewController *)viewController{
    
    UIButton *LeftItem = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    [LeftItem setBackgroundImage:[UIImage imageNamed:@"menusnew"] forState:UIControlStateNormal];
    
    
    viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:LeftItem];
    [LeftItem addTarget:self action:@selector(LeftItemClick) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - navigationBarItem点击事件
-(void)LeftItemClick{
    [self.drawer open];
}

- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}

-(void)addChildViewController:(UIViewController *)childController titles:(NSString *)titles images:(NSString *)imageName{
    
    if (titles) {
        childController.title = titles;
        [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]} forState:UIControlStateNormal];
        [childController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor hm_colorWithRed:28 green:193 blue:155]} forState:UIControlStateSelected];
    }
    if (imageName) {
        childController.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        NSString *SelectImage =[NSString stringWithFormat:@"%@_selected",imageName] ;
        
        childController.tabBarItem.selectedImage = [[UIImage imageNamed:SelectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
    
    JPNavigationController *KYNav = [[JPNavigationController alloc]initWithRootViewController:childController];
    
    [self addChildViewController:KYNav];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
